package com.richman.view;



import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import com.richman.controller.DecreseRatio;



public class ForbidLeagueFrame extends JFrame {

	
	JPanel jpc ;//存放组件的面板
	
	Vector<String> leagues = new Vector<String>();
	JCheckBox all = new JCheckBox("全选");
	JCheckBox neg = new JCheckBox("反选");
	
	JButton refreshBtn;
	
	Vector<String> forbidLeagues = new Vector<String>();
	
	
	private void readLeagueMatch() {
		File file = new File("data/" + "forbidLeague.txt");
		
		if(!file.exists()) {
			System.out.println("[forbidLeague] 文件不存在");
			return;
		}
		
		BufferedReader freader = null;
		try {
			freader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			
			String str = "";
			while ((str = freader.readLine()) != null) {
				if(str.length()>=2) {
					forbidLeagues.add(str);
				}
				
			}
			
			freader.close();
			
			//updateShowItem();
			
			
			
		} catch(FileNotFoundException e) {
			e.printStackTrace();
			return;
		} catch(IOException e) {
			e.printStackTrace();
			return;
		}
					
	}
	
	
	private void updateLeagueMatch() {
		
		try{	
			File file = new File("data/" + "forbidLeague.txt");

			if(!file.exists()){	
				file.createNewFile();
			}
			
			BufferedWriter fwlocal = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
			
			for(String  obs : forbidLeagues) {
				fwlocal.append(obs);
				fwlocal.newLine();
				fwlocal.flush();
			}

			fwlocal.close();
		}catch(FileNotFoundException e) {
			e.printStackTrace();
			return;
		} catch(IOException e) {
			e.printStackTrace();
			return;
		}
		
	}
	
	 public ForbidLeagueFrame() {

		 
		 readLeagueMatch();
			jpc = new JPanel();
		    jpc.setLayout(new BoxLayout(jpc,  BoxLayout.Y_AXIS));//盒子布局.从上到下
		 
		    JScrollPane jsp;//滚动面板
		    //add(jpc);
		    
		    jsp = new JScrollPane(jpc,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	        add(jsp);
//	        
	        jpc.add(all);
	        jpc.add(neg);
	        
	        
	        //generateItem();
	        
	        refreshBtn = new JButton("保存");
	        
	        refreshBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {//保存
					 
					 Component[] cps = jpc.getComponents();
					 
					 forbidLeagues.clear();
					 
					 for(int i = 0; i <cps.length;i++ ) {
						 
						 if(cps[i].getClass().toString().contains("JCheckBox")) {
							 JCheckBox jc = (JCheckBox)cps[i];
							 boolean b = jc.isSelected();
							 if(b==true&&!jc.getText().equals("全选")&&!jc.getText().equals("反选")) {					
									 forbidLeagues.add(jc.getText());

							 }
						 }
					 }					 
					 updateLeagueMatch();	
					 
					 setVisible(false);
				}
				}
	        );

	        JPanel jps = new JPanel();
	        jps.add(refreshBtn);
	        add(jps,BorderLayout.SOUTH);
	        
	        setResizable(false);
		    
	        setTitle("联赛选择");
	        setDefaultCloseOperation( javax.swing.WindowConstants.HIDE_ON_CLOSE);
	        setSize(300, 600);//大小
	        
	        all.addItemListener(new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent e) {
					// TODO Auto-generated method stub
	               // int index = jcb.getSelectedIndex();
					if(e.getStateChange() == ItemEvent.DESELECTED){
						
					}else{
						neg.setSelected(false);
						selectAll();
					}
				}
	        });
	        
	        neg.addItemListener(new ItemListener() {


				@Override
				public void itemStateChanged(ItemEvent e) {
					// TODO Auto-generated method stub
	               // int index = jcb.getSelectedIndex();
					if(e.getStateChange() == ItemEvent.DESELECTED){
						
					}else{
						all.setSelected(false);
						selectneg();
					}
					
					
				}
	        });
	        

	    }
	 
	 public void generateItem() {
		 
		 Vector<String[]> datas = DecreseRatio.mainWnd.getISNPS38LeaguesList();
		 
		 for(int i = 0; i < forbidLeagues.size();i++) {
			 leagues.add(forbidLeagues.elementAt(i));
			 JCheckBox jc = new JCheckBox(forbidLeagues.elementAt(i));
			 jc.setSelected(true);
			 jpc.add(jc);
		 }
		 
		 for(int i = 0; i < datas.size();i++) {
			 if(!leagues.contains(datas.elementAt(i)[0])) {
				 leagues.add(datas.elementAt(i)[0]);
				 if(datas.elementAt(i)[0].length()<2) {
					 continue;
				 }
				 JCheckBox jc = new JCheckBox(datas.elementAt(i)[0]);
				 jpc.add(jc);
			 }
		 }		 
		 myUpdateUI();

	 }
	 
	    private void myUpdateUI() {
	        SwingUtilities.updateComponentTreeUI(this);//添加或删除组件后,更新窗口

	    }
	 
	 public Vector<String> getForbidLeagues() {
		 
		 Vector<String> localleagues = new Vector<String>();		 
		 Component[] cps = jpc.getComponents();
		 for(int i = 0; i <cps.length;i++ ) {			 
			 if(cps[i].getClass().toString().contains("JCheckBox")) {
				 JCheckBox jc = (JCheckBox)cps[i];
				 boolean b = jc.isSelected();
				 if(b==true) {
					 localleagues.add(jc.getText());
				 }
			 }
		 }
		 return localleagues;
	 
	 }
	 
	 public void selectAll() {
		 Component[] cps = jpc.getComponents();
		 for(int i = 0; i <cps.length;i++ ) {
			 
			 if(cps[i].getClass().toString().contains("JCheckBox")) {
				 JCheckBox jc = (JCheckBox)cps[i];
				 boolean b = jc.isSelected();
				 if(!jc.getText().equals("反选")) {
					 jc.setSelected(true);
				 }
			 }
			 
			 
		 }
	 }
	 
	 public void selectneg() {
		 Component[] cps = jpc.getComponents();
		 for(int i = 0; i <cps.length;i++ ) {
			 
			 if(cps[i].getClass().toString().contains("JCheckBox")) {
				 JCheckBox jc = (JCheckBox)cps[i];				 
				 if(!jc.getText().equals("全选")) {
					 
					 jc.setSelected(!jc.isSelected());
				 }
			 }
			 
			 
		 }
	 }
	
}

